﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinchoo.Core;
using System.IO;
using Cinchoo.Core.Diagnostics;
using Cinchoo.Core.ETL;
using ChoWorkflowServer.Model;
using System.Threading;
using Cinchoo.Core.Services;

namespace ChoWorkflowServer
{
    public class ChoJobStore : IChoETLJobStore
    {
        private readonly IChoDataModel _dataModel = ChoDataModelFactory.New();
        private readonly object _padLock = new object();
        private readonly Dictionary<string, IChoETLCalendar> _calendars = new Dictionary<string, IChoETLCalendar>();

        public ChoJobStore()
        {
            ChoGlobalTimerServiceManager.Register("WF_Calendar", () =>
                {
                    lock (_padLock)
                    {
                        _calendars.Clear();
                    }

                }, 60 * 60 * 1000);
        }

        public IChoETLCalendar RetrieveCalendar(string calName)
        {
            if (calName.IsNullOrWhiteSpace()) 
                return null;

            if (_calendars.ContainsKey(calName))
                return _calendars[calName];

            lock (_padLock)
            {
                if (!_calendars.ContainsKey(calName))
                {
                    if (calName == ChoCalendar.NULL_CALENDAR_NAME)
                    {
                        _calendars.Add(calName, ChoETLNullCalendar.Default);
                    }
                    else
                    {
                        ChoETLHolidayCalendar calCalendar = new ChoETLHolidayCalendar();

                        ChoActionWaitFor waitFor = new ChoActionWaitFor(() =>
                            {
                                _dataModel.GetAllHolidayCalendarDatesByName(calName);

                                calCalendar.ExcludedDates.Clear();
                                ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable table = _dataModel.GetAllHolidayCalendarDatesByName(calName);
                                foreach (ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow row in table)
                                {
                                    calCalendar.ExcludedDates.Add(row.CAL_DATE);
                                }
                            });

                        try
                        {
                            waitFor.Run(Timeout.Infinite, 3);
                            _calendars.Add(calName, calCalendar);
                        }
                        catch
                        {
                            return null;
                        }
                    }
                }
                return _calendars[calName];
            }
        }

        public void ResetCalendar(string calName)
        {
            if (calName.IsNullOrWhiteSpace()) return;

            if (!_calendars.ContainsKey(calName))
                return;

            lock (_padLock)
            {
                if (!_calendars.ContainsKey(calName))
                    return;

                _calendars.Remove(calName);
            }
        }
    }
}
